package com.hmdp.consume;

import com.hmdp.config.RabbitMQConfig;
import com.hmdp.dto.VoucherOrderMessageDTO;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherMapper;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.utils.RedisIdWorker;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 发送消息消费者
 */
@Component
@RabbitListener(queues = {RabbitMQConfig.MSG_QUEUE})
public class MsgSendConsumer {

    @Autowired
    private RedisIdWorker redisIdWorker;

    @Autowired
    private VoucherOrderMapper voucherOrderMapper;

    /**
     * 保存订单
     * @param messageDTO
     */
    @RabbitHandler
    public void handleMessage(VoucherOrderMessageDTO messageDTO) {
        Long orderId = redisIdWorker.nextId("order");
        VoucherOrder voucherOrder = new VoucherOrder();
        voucherOrder.setId(orderId);
        voucherOrder.setUserId(messageDTO.getUserId());
        voucherOrder.setVoucherId(messageDTO.getVoucherId());
        voucherOrderMapper.insert(voucherOrder);
    }

}
